package com.xcf.controller.business.integrateddata;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xcf.controller.base.BaseController;
import com.xcf.service.business.integrateddata.BookingCargoTicketsService;
import com.xcf.util.PageData;
import com.xcf.util.ResponseResult;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@Controller
@RequestMapping(value="/bookingCargoTickets")
public class BookingCargoTicketsController extends BaseController{
	@Autowired
	private BookingCargoTicketsService bookingCargoTicketsService;
	/**
	 * 订舱分票跳转页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	public ModelAndView forwordBookingCargoTickets()throws Exception{
		ModelAndView view = new ModelAndView();
		view.setViewName("/business/integrateddata/booking_cargo_ticekets");
		return view;
	}
	/**
	 * 订舱分票数据查询
	 * @return 
	 * @throws Exception
	 */
	@RequestMapping(value="/findBookingCargoTicketsInfo")
	@ResponseBody
	public ResponseResult<Object> findBookingCargoTicketsInfo(int page,int limit)throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		PageHelper.startPage(page, limit);	
		List<PageData> list = bookingCargoTicketsService.findBookingCargoTickets(pd);
		List<PageData> list1= new ArrayList<>();
		//JSONArray json = new JSONArray();
		for(PageData p : list){
			 p.put("ID", p.get("ID"));
			 p.put("BILL_CODE", p.getString("BILL_CODE"));
			 p.put("LADING_BILL_CODE", p.getString("LADING_BILL_CODE"));
			 if(p.get("IS_MAIN_LADING_BILL")!=null && p.get("IS_MAIN_LADING_BILL")!=""){
				 int i=Integer.parseInt(p.get("IS_MAIN_LADING_BILL").toString());
			 if(i==1){
				 p.put("IS_MAIN_LADING_BILL", "是"); 
			 }else if(i==0){
				 p.put("IS_MAIN_LADING_BILL", "否"); 
			 }}
			 if(p.get("IS_CUSTOMS")!=null && p.get("IS_CUSTOMS")!=""){
				 int i=Integer.parseInt(p.get("IS_CUSTOMS").toString());
			if(i==1){
				 p.put("IS_CUSTOMS","是");
			}else if(i==0){
				 p.put("IS_CUSTOMS", "否");
			}}
			 if(p.get("IS_SIGN_BILL")!=null && p.get("IS_SIGN_BILL")!=""){
				 int i=Integer.parseInt(p.get("IS_SIGN_BILL").toString());
			if(i==1){
				p.put("IS_SIGN_BILL", "是");
			}else if(i==0){
				p.put("IS_SIGN_BILL", "否");
			}
			 }
			 p.put("BOX_MASTER", p.get("BOX_MASTER"));
	            list1.add(p);
		}
      	return ResponseResult.success(new PageInfo(list));
	}
	/**
	 * 根据托单编号查询订舱分票
	 * @throws Exception
	 */
	@RequestMapping(value="/findBookingCargoTicketsInfoByBillCode")
	public void findBookingCargoTicketsInfoByBillCode(HttpServletResponse response)throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		List<PageData> tickets = bookingCargoTicketsService.findBookingCargoTicketsByBillCode(pd);
		JSONArray json = new JSONArray();
		for(PageData p : tickets){
			 JSONObject jo = new JSONObject();

			 jo.put("ID", p.get("ID"));
			 jo.put("BILL_CODE", p.getString("BILL_CODE"));
			 jo.put("LADING_BILL_CODE", p.getString("LADING_BILL_CODE"));
			 if(p.get("IS_MAIN_LADING_BILL")!=null && p.get("IS_MAIN_LADING_BILL")!=""){
				 int i=Integer.parseInt(p.get("IS_MAIN_LADING_BILL").toString());
			 if(i==1){
				 jo.put("IS_MAIN_LADING_BILL", "是"); 
			 }else if(i==0){
				 jo.put("IS_MAIN_LADING_BILL", "否"); 
			 }}
			 if(p.get("IS_CUSTOMS")!=null && p.get("IS_CUSTOMS")!=""){
				 int i=Integer.parseInt(p.get("IS_CUSTOMS").toString());
			if(i==1){
				 jo.put("IS_CUSTOMS","是");
			}else if(i==0){
				 jo.put("IS_CUSTOMS", "否");
			}}
			 if(p.get("IS_SIGN_BILL")!=null && p.get("IS_SIGN_BILL")!=""){
				 int i=Integer.parseInt(p.get("IS_SIGN_BILL").toString());
			if(i==1){
				jo.put("IS_SIGN_BILL", "是");
			}else if(i==0){
				jo.put("IS_SIGN_BILL", "否");
			}
			 }
			 jo.put("BOX_MASTER", p.get("BOX_MASTER"));
	            json.add(jo);
		}
		response.setContentType("text/html; charset=utf-8");
		response.getWriter().write("{"+"\""+"total"+"\""+":"+"\""+"5"+"\""+","+"\""+"root1"+"\""+":"+json.toString()+"}");				
		response.getWriter().flush();
	}
	/**
	 * 保存或修改数据
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/saveAndUpdateInfo")
	@ResponseBody
	public String saveAndUpdateInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();
		String data = pd.getString("array");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			PageData pdq = new PageData();
			if(item.getString("IS_MAIN_LADING_BILL").equals("1")){
				pdq.put("BILL_CODE", item.getString("BILL_CODE"));
				pdq.put("LADING_BILL_CODE", item.getString("LADING_BILL_CODE"));

					pdq.put("IS_MAIN_LADING_BILL", item.getString("IS_MAIN_LADING_BILL"));	

					pdq.put("IS_CUSTOMS", item.getString("IS_CUSTOMS"));	

					pdq.put("IS_SIGN_BILL",item.getString("IS_SIGN_BILL"));	

				pdq.put("BOX_MASTER", item.getString("BOX_MASTER"));
				//如果ID为空则保存到分票，不为空更新托单表
				if(item.getString("ID").isEmpty()){
					bookingCargoTicketsService.saveInfo(pdq);
				}else{
					bookingCargoTicketsService.updateBookNoteInfo(pdq);
				}
			}else if(item.getString("IS_MAIN_LADING_BILL").equals("0")){
				pdq.put("BILL_CODE", item.getString("BILL_CODE"));
				pdq.put("LADING_BILL_CODE", item.getString("LADING_BILL_CODE"));

					pdq.put("IS_MAIN_LADING_BILL", item.getString("IS_MAIN_LADING_BILL"));	

					pdq.put("IS_CUSTOMS", item.getString("IS_CUSTOMS"));	

					pdq.put("IS_SIGN_BILL", item.getString("IS_SIGN_BILL"));	

				pdq.put("CONTAINER_OWNER", item.getString("BOX_MASTER"));
				//如果ID为空则保存到分票，不为空更新分票表
				if(item.getString("ID").isEmpty()){
					bookingCargoTicketsService.saveInfo(pdq);
				}else{
					bookingCargoTicketsService.updateInfo(pdq);
				}
				

			}
		}
		return null;
		
	}
	/**
	 * 删除信息
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteInfo")
	@ResponseBody
	public String deleteInfo()throws Exception{
		PageData pd = new PageData();
		pd = this.getPageData();

		String data = pd.getString("array");

		JSONArray items = JSONArray.fromObject(data);
		for(int i=0,size=items.size();i<size;i++){
			JSONObject item = items.getJSONObject(i);
			String id = item.getString("id").trim();
			PageData pdq = new PageData();
			pdq.put("ID", id);
			bookingCargoTicketsService.deleteInfo(pdq);
		}
		return null;
		
	}
}
